<?php

/*
 * This class is used for as Model for Project table 
 */

class Model_Project extends Model_Base {

    protected $_name = 'tbl_project';
    protected $_primaryKey = 'prj_id';

    /**
     * Function to return different status of the Project
     *
     * @param  null
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function getStatusArray() {
        return array(
            '' => '-- Select Status --',
            'active' => 'Active',
            'inactive' => 'Inactive'
        );
    }

    /**
     * Function to return different commission type of the Project
     *
     * @param  null
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function getCommisionTypeArray() {
        return array(
            '' => '-- Select Commision type --',
            'lumpsum' => 'Lumpsum',
            'percentage' => 'Percentage'
        );
    }

    /**
     * Function to return different image type of the Project
     *
     * @param  null
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function getImageTypeArray() {
        return array(
            '3d' => '3d',
            'architecture' => 'Architechture',
            'progress' => 'Progress'
        );
    }

    /**
     * Function to return deisgner
     *
     * @param  null
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function getDesignerArray() {
        //Load Designer Model
        $designerModel = new Model_Designer();
        $designerArray = $designerModel->fetchDesigners();
        $designers = Array();
        if (count($designerArray) > 0) {
            foreach ($designerArray as $_designer) {
                $designers[$_designer['dsg_id']] = $_designer['dsg_name'];
            }
        }
        return $designers;
    }

    /**
     * Function to return contractor
     *
     * @param  null
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function getContractorArray() {
        //Load Contractor Model
        $contractors = Array();
        $contractorModel = new Model_Contractor();
        $contractorData = $contractorModel->fetchContractors();
        foreach ($contractorData as $_ctr) {
            $contractors[$_ctr['ctr_id']] = $_ctr;
        }
        return $contractors;
    }

    /**
     * Function to return customer name
     *
     * @param  null
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function getCustomersArray() {
        //Load Customer Model
        $customerModel = new Model_Customer();
        $customerArray = $customerModel->fetchCustomers();
        $customers = Array('' => '-- Select Customer --');
        if (count($customerArray) > 0) {
            foreach ($customerArray as $_customer) {
                $customers[$_customer['cus_id']] = $_customer['cus_name'];
            }
        }
        return $customers;
    }

    /**
     * Fetch an individual entry
     *
     * @param  int|string $id
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function fetchEntryById($id) {
        $select = $this->select()
                ->from(array('project' => 'tbl_project'))
                ->where('prj_id = ?', $id);
        // see reasoning in fetchEntries() as to why we return only an array
        #$projectData = $this->fetchRow($select);
        #
        // see reasoning in fetchEntries() as to why we return only an array
        $projectData = $this->fetchRow($select);

        if (!empty($projectData)) {
            $project = $projectData->toArray();

            $prj_id = $project['prj_id'];

            //Load Project Reference Model
            $projectReferenceModel = new Model_Projectreference();
            $projectReference = $projectReferenceModel->fetchEntryById($prj_id);

            //Load Project Image Model
            $projectImageModel = new Model_Projectimages();
            $projectImage = $projectImageModel->fetchEntryById($prj_id);

            return Array(
                'PROJECT' => $project,
                'PROJECT_REFERENCE' => $projectReference,
                'PROJECT_IMAGES' => $projectImage,
            );
        } else {
            return Array(
                'PROJECT' => Array(),
                'PROJECT_REFERENCE' => Array(),
                'PROJECT_IMAGES' => Array(),
            );
        }
    }

    /**
     *  This function is used to get projects
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function fetchProjects($where = "", $limit = "", $offset = "0", $order = 'prj_id', $sort = 'ASC') {
        $select = $this->select()
                ->from(array('prj' => 'tbl_project'))
                ->setIntegrityCheck(false)
                ->join(array('dsg' => 'tbl_designer'), "dsg.dsg_id = prj.dsg_id")
                ->join(array('cus' => 'tbl_customer'), "cus.cus_id = prj.cus_id");
        if ($where != '') {
            $select->where($where);
        }
        if ($limit != "") {
            $select->limit($limit, $offset);
        }
        $select->order($order . " " . $sort);
        
        $data = parent::fetchAll($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }

    /**
     *  This function is used to get total project
     * @return int
     */
    public function getProjectCount($where) {
        $select = $this->select()
                ->from(array('prj' => 'tbl_project'), Array('cnt' => 'count(*)'))
                ->setIntegrityCheck(false)
                ->join(array('dsg' => 'tbl_designer'), "dsg.dsg_id = prj.dsg_id")
                ->join(array('cus' => 'tbl_customer'), "cus.cus_id = prj.cus_id");
        if ($where != '') {
            $select->where($where);
        }
        $rows = parent::fetchRow($select);
        return($rows->cnt);
    }

    /**
     *  This function is used to delete projects
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function deleteProject($projecttArray) {
        if (count($projecttArray) <= 0) {
            return;
        }

        foreach ($projecttArray as $_project) {
            $config = Zend_Registry::get('config');
            $projectData = $this->fetchEntryById($_project);
            $project = $projectData['PROJECT'];

            //Delete Project table entry
            $this->delete('prj_id = ' . (int) $project['prj_id']);

            //Load Project referece table 
            $projectReferenceModel = new Model_Projectreference();
            //Delete Project Reference table entry
            $projectReferenceModel->delete('prj_id = ' . (int) $project['prj_id']);

            //Load Project Image table 
            $projectImageModel = new Model_Projectimages();

            //Delete Project Image table entry
            $projectImageModel->delete('prj_id = ' . (int) $project['prj_id']);

            //Delete the Images
            $projectImages = $projectData['PROJECT_IMAGES'];
            $folder = $config->UPLOAD_PATH . "project/";
            foreach ($projectImages as $_images) {

                #print_R($_images);
                #exit;
                //Deletion of the images
                $image = $folder . $_images['prji_image'];
                $thumb_image = $folder . "thumb_" . $_images['prji_image'];
                if (file_exists($image)) {
                    @unlink($image);
                }
                if (file_exists($thumb_image)) {
                    @unlink($thumb_image);
                }
            }
        }
    }

    /**
     *  This function is used to get projects by customers
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function fetchProjectsByCustomer($cus_id = "") {
        $select = $this->select()
                ->from(array('prj' => 'tbl_project'))
                ->where("prj.prj_status = 'active'");
        if ($cus_id != '') {
            $select->where('prj.cus_id = ?', $cus_id);
        }
        $data = parent::fetchAll($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }

}
